<template>
  <div>
    <!-- 头部导航 -->
    <div>
      <van-nav-bar fixed>
        <template #left>
          <img src="@/assets/logo.png" class="logo" />
        </template>
        <template #right>
          <van-icon
            name="search"
            size="0.48rem"
            color="#fff"
            @click="searchFn"
          />
        </template>
      </van-nav-bar>
    </div>
    <!-- 导航栏 -->
    <div class="myBar">
      <!-- 导航分类 -->
      <van-tabs v-model="chanId" sticky animated offset-top="46px">
        <van-tab
          v-for="channel in channelUserList"
          :key="channel.id"
          :title="channel.name"
          :name="channel.id"
        >
          <ArticleList :chanId="chanId"></ArticleList>
        </van-tab>
      </van-tabs>
      <!-- 小+号 -->
      <van-icon
        name="plus"
        size="0.37333334rem"
        class="moreChannels"
        @click="changeChannels"
      />
      <!-- 弹出层 -->
      <van-popup v-model="show" get-container="body" class="myPopup">
        <ChannelEdit
          :channelUserList="channelUserList"
          :uncheckChannelList="uncheckChannelList"
          @addChannelEV="addChannel"
          @removeChannlEV="removeChannl"
          @closeEV="closeChannel"
          v-model="chanId"
        ></ChannelEdit>
      </van-popup>
    </div>
  </div>
</template>

<script>
import { reqUserChannels, reqAllChannels, reqUpdateChannels } from "@/api";
import ArticleList from "./component/ArticleList";
import ChannelEdit from "./ChannelEdit";
export default {
  name: "Home",
  data() {
    return {
      chanId: 0,
      channelUserList: [],
      channelAllList: [],
      show: false,
    };
  },
  computed: {
    uncheckChannelList() {
      const newArr = this.channelAllList.filter((item) => {
        let index = this.channelUserList.findIndex((item2) => {
          return item.id === item2.id;
        });
        if (index > -1) {
          return false;
        } else {
          return true;
        }
      });
      return newArr;
    },
  },
  components: {
    ArticleList,
    ChannelEdit,
  },
  async created() {
    const res = await reqUserChannels();
    const res2 = await reqAllChannels();
    if (res.status == 200) {
      this.channelUserList = res.data.data.channels;
    }
    if (res2.status == 200) {
      this.channelAllList = res2.data.data.channels;
    }
  },
  methods: {
    changeChannels() {
      this.show = true;
    },
    addChannel(newChannel) {
      this.channelUserList.push(newChannel);
      this.updateChannels();
    },
    async removeChannl(mychannel) {
      let index = this.channelUserList.findIndex(
        (obj) => obj.id === mychannel.id
      );
      this.channelUserList.splice(index, 1);
      this.updateChannels();
    },
    async updateChannels() {
      const newArr = this.channelUserList.filter((obj) => obj.id !== 0);
      const theArr = newArr.map((obj, index) => {
        const newObj = { ...obj };
        delete newObj.name;
        newObj.seq = index;
        return newObj;
      });
      try {
        const res = await reqUpdateChannels(theArr);
      } catch (error) {
        alert(error.message);
      }
    },
    closeChannel() {
      this.show = false;
    },
    searchFn() {
      this.$router.push({
        path: "/search",
      });
    },
  },
};
</script>

<style lang="less" scoped>
.logo {
  width: 100px;
  height: 30px;
}
.myBar {
  margin-top: 46px;
}
// 设置 tabs 容器的样式
/deep/ .van-tabs__wrap {
  padding-right: 30px;
  background-color: #fff;
}

// 设置小图标的样式
.moreChannels {
  position: fixed;
  top: 62px;
  right: 8px;
  z-index: 999;
}

.myPopup {
  width: 100vw;
  height: 100vh;
}
</style>
